/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.attendance.clazz.service.impl;

import com.arisgo.cloud.attendance.clazz.entity.GroupStaff;
import com.arisgo.cloud.attendance.clazz.mapper.GroupStaffMapper;
import com.arisgo.cloud.attendance.clazz.service.GroupStaffService;
import com.arisgo.cloud.attendance.clazz.vo.GroupStaffVo;
import com.arisgo.cloud.attendance.clazz.vo.StaffVo;
import com.arisgo.cloud.common.Pagination;
import com.arisgo.cloud.common.utils.AssertUtil;
import com.arisgo.cloud.web.mybatis.service.CloudServiceImpl;
import com.arisgo.cloud.web.param.Paging;
import com.arisgo.cloud.web.param.Param;
import com.arisgo.cloud.web.param.Search;
import com.arisgo.cloud.web.utils.RequestUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 排班组员工
 *
 * @author Coulson
 * @since 2025-03-26
 */
@Service
public class GroupStaffServiceImpl extends CloudServiceImpl<GroupStaffMapper, GroupStaff, GroupStaffVo> implements GroupStaffService {

    @Override
    public GroupStaff getInstanceModel() {
        return new GroupStaff();
    }

    @Override
    public GroupStaffVo getInstanceVo() {
        return new GroupStaffVo();
    }

    @Override
    public Pagination<StaffVo> findByGroupId(Long groupId, Paging paging) {
        paging.addTenantIdParam(RequestUtil.getTenantId());
        paging.addParam(Param.creator(GroupStaff.GROUP_ID, groupId));
        Map<String, Object> params = paging.convertMap();
        PageHelper.startPage(paging.getPageNo(), paging.getPageSize());
        PageInfo<StaffVo> page = new PageInfo<>(this.mapper.findStaffByGroupId(params));
        return Pagination.creator(page.getPageNum(), page.getPageSize(), page.getTotal(), page.getList());
    }

    @Override
    public boolean removeByGroupId(Long groupId, List<Long> staffIds) {
        AssertUtil.notEmpty(staffIds, "员工为空！");
        QueryWrapper wrapper = new QueryWrapper();
        Search search = Search.creator(Param.creator(GroupStaff.GROUP_ID, groupId), Param.creator(GroupStaff.STAFF_ID, Param.Expr.IN.name(), staffIds));
        search.addTenantIdParam(RequestUtil.getTenantId());
        search.buildWrapper(wrapper);
        this.mapper.deleteByQuery(wrapper);
        return true;
    }
}
